﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
namespace EthnicEdu.Pages
{
    public partial class UploaderCreateQuestion : PhoneApplicationPage
    {
        public UploaderCreateQuestion()
        {
            InitializeComponent();


            List<Theme> lsImg = new List<Theme>();
            lsImg.Add(new Theme("Class",1));
            lsImg.Add(new Theme("School",2));
            lsImg.Add(new Theme("Room",3));
            lsImg.Add(new Theme("Garden",4));
            lsImg.Add(new Theme("Street",5));
            lsImg.Add(new Theme("Store",6));
            lsImg.Add(new Theme("Museum",7));
            lsImg.Add(new Theme("PoliceStation", 8));
            lpkIcon.ItemsSource = lsImg;
            
            List<eThicData.Category> lsCate = new List<eThicData.Category>();
            lsCate.Add(new eThicData.Category() {  CategoryId =1, CategoryName = "Study"});
            lsCate.Add(new eThicData.Category() { CategoryId = 2, CategoryName = "Home" });
            lsCate.Add(new eThicData.Category() { CategoryId = 3, CategoryName = "Social" });

            lpkCate.ItemsSource = lsCate;
            
        }

        

        public class Theme
        {
            public string Name { get; set; }
            public int ThemeID { get; set; }
            public Theme(string Name , int ID)
            {
                this.Name = Name;
                this.ThemeID = ID;
            }
        }
       
        private void btnAdd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
               
                txtAnswer.Text = txtAnswer.Text.Trim();
                if (!string.IsNullOrEmpty(txtAnswer.Text))
                {
                    if (!lstAnswer.Items.Contains(txtAnswer.Text))
                    {
                        lstAnswer.Items.Add(txtAnswer.Text);
                        txtAnswer.Text = string.Empty;
                    }
                    else
                    {
                        MessageBox.Show("Trung Dap An");
                    }
                }

                
            }
            catch {
               
            }
        }



     
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {
               
                if (!string.IsNullOrWhiteSpace(txtTitle.Text) && !string.IsNullOrWhiteSpace(txtContent.Text) && lstAnswer.Items.Count >= 2)
                {
                    pbLoading.IsIndeterminate = true;
                    
                    ObservableCollection<string> Answers = new ObservableCollection<string>();
                    foreach (string item in lstAnswer.Items)
                    {
                        Answers.Add(item);
                    }
                    App.myService.AddQuestionAndListAnswersAsync(txtTitle.Text, txtContent.Text, App.AccountID, (lpkCate.SelectedItem as eThicData.Category).CategoryId, (lpkIcon.SelectedItem as Theme).ThemeID, Answers);
                    App.myService.AddQuestionAndListAnswersCompleted += new EventHandler<Service.AddQuestionAndListAnswersCompletedEventArgs>(myService_AddQuestionAndListAnswersCompleted);
                }
                else
                {
                    MessageBox.Show("Nhap du lieu khong hop le");
                }
            }
            catch {
                pbLoading.IsIndeterminate = false;
                //btnSubmit.IsEnabled = true;
            }
        }

        void myService_AddQuestionAndListAnswersCompleted(object sender, Service.AddQuestionAndListAnswersCompletedEventArgs e)
        {
            pbLoading.IsIndeterminate = false;
            MessageBox.Show("Thanh Cong");
            pbLoading.IsIndeterminate = false;
        

        }

        private void clear_Click(object sender, EventArgs e)
        {
            txtAnswer.Text = string.Empty;
            txtContent.Text = string.Empty;
            txtTitle.Text = string.Empty;
            lstAnswer.Items.Clear();
        }

        private void lpkIcon_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (lpkIcon.ListPickerMode == ListPickerMode.Full)
                lpkIcon.Open();
        }
    }
}